<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 核心模块-网站平台
 */
class siteConfig extends AdminController {

	public $_validation = null;

	function __construct(){
		parent::__construct();
	}
	
	//编辑网站基础配置
	function index(){
		$this->load->model('MSiteConfig');
		$this->load->model('MSiteConfigDef');
		$defs = $this->MSiteConfigDef->getAll();
		$params = $this->MSiteConfig->getAll();
		$confs = Array();
		foreach ($params as $val){
			$confs[$val->config_key] = $val->value;
		}
		
		$postInfo = $this->input->post();
		if(!$postInfo){
			$postInfo = array();
		}
		
		foreach ($defs as $def){
			if($def->data_type == '2'){
				$arr = Array();
				//下拉框
				if(isset($def->data_extra) && strlen($def->data_extra) > 0){
					$tmpData = explode(';', $def->data_extra);
					foreach ($tmpData as $item){
						$itemSub = explode('=', $item);
						if(count($itemSub) > 1){
							$arr[$itemSub[0]] = $itemSub[1];
						}else if(count($itemSub) == 1){
							$arr[$itemSub[0]] = $itemSub[0];
						}
					}
				}
				$def->selectList = $arr;
			}
			$def->config_value = '';
			if(array_key_exists($def->config_key, $confs)){
				$def->config_value = $confs[$def->config_key];
			}
			$def->value = $def->config_value;
			if(array_key_exists($def->config_key, $postInfo)){
				$def->value = $postInfo[$def->config_key];
			}
		}
		
		if($this->p->edit && $postInfo){
				if($this->MSiteConfig->saveConfig($defs)){
					successAndRedirect(l('edit_success'));
				}else{
					errorAndRedirect(l('data_fail'));
				}
		}
		$this->assign('defs',$defs);
		$this->layout();
	}
	
}
